package com.zlf.controller;

import com.zlf.po.CommonResult;
import com.zlf.po.Orders;
import com.zlf.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RefreshScope  //开启动态刷新
@RequestMapping("/OrdersController")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @PostMapping("/createOrders/{userId}/{businessId}/{daId}/{orderTotal}")
    public CommonResult<Integer> createOrders(
            @PathVariable("userId") String userId,
            @PathVariable("businessId") Integer businessId,
            @PathVariable("daId") Integer daId,
            @PathVariable("orderTotal") Double orderTotal) throws Exception{
        Orders orders = new Orders();
        orders.setUserId(userId);
        orders.setBusinessId(businessId);
        orders.setDaId(daId);
        orders.setOrderTotal(orderTotal);
        int orderId = ordersService.createOrders(orders);
        if (orderId==-403){
            return new CommonResult(403,"fegin触发了熔断降级",null);
        }
        return new CommonResult(200,"success（10600）",orderId);
    }

    @GetMapping("/getOrdersById/{orderId}")
    public CommonResult<Orders> getOrdersById(
            @PathVariable("orderId") Integer orderId) throws Exception{
        Orders orders = ordersService.getOrdersById(orderId);
        return new CommonResult(200,"success（10600）",orders);
    }

    @GetMapping("/listOrdersByUserId/{userId}")
    public CommonResult<List> listOrdersByUserId(
            @PathVariable("userId") String userId) throws Exception{
        List<Orders> list = ordersService.listOrdersByUserId(userId);
        return new CommonResult(200,"success（10600）",list);
    }

    //待完善
    @RequestMapping("/payOrdersById")
    public int payOrdersById(int orderId) throws Exception{
        return ordersService.payOrdersById(orderId);
    }
    @RequestMapping("/cancelOrdersById")
    public int cancelOrdersById(int orderId) throws Exception{
        return ordersService.cancelOrdersById(orderId);
    }

    @RequestMapping("/listOrdersByUserIdByState")
    public List<Orders> listOrdersByUserIdByState(Orders orders,int orderState) throws Exception{
        return ordersService.listOrdersByUserIdByState(orders.getUserId(),orderState);
    }
}

